package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.db.FindDB;
import com.luck.easyfoodbatch.mvp.mine.AddAddressModel;
import com.luck.easyfoodbatch.mvp.mine.AddAddressPresenter;
import com.luck.easyfoodbatch.mvp.mine.AddAddressView;
import com.luck.easyfoodbatch.mvp.mine.UpdateAddressModel;
import com.luck.easyfoodbatch.mvp.mine.UpdateAddressPresenter;
import com.luck.easyfoodbatch.mvp.mine.UpdateAddressView;
import com.luck.easyfoodbatch.mvp.model.AddressModel;
import com.luck.easyfoodbatch.mvp.model.MyAddress;
import com.luck.easyfoodbatch.pickercity.PopCityHelper;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewlyAddedActivity extends BaseActivity implements AddAddressView, UpdateAddressView {

    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.et_uname)
    EditText etUname;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.ll_city)
    LinearLayout ll_city;
    private String title = "";
    private AddAddressModel addressModel;
    private UpdateAddressModel updateAddressModel;
    private PopCityHelper cityHelper;
    private String province, city, town, address_id;
    private int type = 0;
    private int address_type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type", 0);
        address_type = getIntent().getIntExtra("address_type", 1);
        switch (type) {
            case 0:
                title = "新增地址";
                break;
            case 1:
                title = "修改地址";
                break;
        }
        setContentView(R.layout.activity_newly_added, title, "保存", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        if (type == 1) {
            MyAddress.DataBean bean = (MyAddress.DataBean) getIntent().getSerializableExtra("bean");
            etUname.setText(bean.getUname());
            etPhone.setText(bean.getMobile());
            tvAddress.setText(bean.getAddress_str());
            etNumber.setText(bean.getStreet());
            province = bean.getProvince();
            city = bean.getCity();
            town = bean.getTown();
            address_id = bean.getAddress_id();
        }
        cityHelper = new PopCityHelper(this);
        cityHelper.setOnClickOkListener(new PopCityHelper.OnClickOkListener() {
            @Override
            public void onClickOk(String birthday, String pid, String cid, String aid) {
                tvAddress.setText(birthday);
                province = pid;
                city = cid;
                town = aid;
            }
        });
    }

    @OnClick({R.id.right_title, R.id.tv_address})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_title:
                String name = etUname.getText().toString().trim();
                String phone = etPhone.getText().toString().trim();
                String number = etNumber.getText().toString().trim();
                String address = tvAddress.getText().toString().trim();
                if (Tools.isNull(name)) {
                    T.showToast(mContext, "请输入姓名");
                } else if (Tools.isNull(phone)) {
                    T.showToast(mContext, "请输入联系方式");
                } else if (!Tools.validatePhone(phone)) {
                    T.showToast(mContext, "请输入正确手机号码");
                } else if (Tools.isNull(address)) {
                    T.showToast(mContext, "请选择所在区域");
                } else if (Tools.isNull(number)) {
                    T.showToast(mContext, "请输入门牌号");
                } else {
                    if (type == 1) {
                        // 修改地址
                        showProgressDialog("正在修改，请稍候……");
                        updateAddressModel = new UpdateAddressPresenter(this, mContext);
                        updateAddressModel.updateAddress(address_type + "", address_id, name, phone, province, city, town, number);
                    } else {
                        // 添加地址
                        showProgressDialog("正在添加，请稍候……");
                        addressModel = new AddAddressPresenter(this, mContext);
                        addressModel.addAddress(address_type + "", name, phone, province, city, town, number);
                    }
                }
                break;
            case R.id.tv_address:
                cityHelper.show(v);
                break;
        }

    }

    @Override
    public void addressSuccess(AddressModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            sendBroadcast("action.app.refresh");
            finish();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }


    @Override
    public void updateAddressSuccess(AddressModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            T.showToast(mContext, model.getMsg());
            sendBroadcast("action.app.refresh");
            finish();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }


    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
